<script setup name="Bill">
import {billDel, billGetList, billGetTypeList} from "@/api/fms/bill";
import {accountGetList , accountEditForBillExec} from "@/api/fms/account";
import BillCurd from "@/views/fms/bill/curd";
import UserSearch from "@/views/hrp/user/search";
import {getCurrentInstance, onMounted, reactive} from "vue";
import {useRoute} from "vue-router";

const {proxy} = getCurrentInstance();

const route = useRoute();
const {fms_bill_state} = proxy.useDict('fms_bill_state');

onMounted(() => {
  pd.queryParams.typeId = route.query.typeId;
  billGetTypeList({typeId: pd.queryParams.typeId}).then(res => {
    pd.billType = res.data[0];
  });
  accountGetList({enableFlag: "0"}).then(res => {
    pd.accountList = res.data;
  });
  getList();
});

const pd = reactive({
  billList: [],
  accountList: [],
  billType: {},
  loading: true,
  showSearch: true,
  total: 0,
  daterangePlanTime: [],
  daterangeExecutionTime: [],
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    typeId: null,
    state: null
  },

});

/** 查询合作伙伴列表 */
function getList() {
  pd.loading = true;
  if (null != pd.daterangePlanTime && '' != pd.daterangePlanTime) {
    pd.queryParams.beginPlanTime = pd.daterangePlanTime[0];
    pd.queryParams.endPlanTime = pd.daterangePlanTime[1];
  }
  if (null != pd.daterangeExecutionTime && '' != pd.daterangeExecutionTime) {
    pd.queryParams.beginExecutionTime = pd.daterangePlanTime[0];
    pd.queryParams.endExecutionTime = pd.daterangePlanTime[1];
  }
  billGetList(pd.queryParams).then(res => {
    pd.billList = res.data;
    pd.total = res.total;
    pd.loading = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  pd.queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  pd.queryParams.beginPlanTime = null;
  pd.queryParams.endPlanTime = null;
  pd.queryParams.beginExecutionTime = null;
  pd.queryParams.endExecutionTime = null;
  pd.queryParams.name = null;
  pd.daterangeUpdate_time = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  let bill = {};
  bill.typeId = pd.queryParams.typeId;
  proxy.$refs["billCurdRef"].add(bill);
}

/** 修改按钮操作 */
function handleEdit(bill) {
  proxy.$refs["billCurdRef"].edit(bill);
}

/** 修改按钮操作 */
function handleDel(id) {
  proxy.$refs["billCurdRef"].del(id);
}

/** 对话框完成后刷新界面 */
function billCurdOk(operation) {
  getList();
}

function billExec(id){
  proxy.$modal.confirm('是否确认执行财务单据编号为:"' + id + '"的数据项？').then(function () {
    return accountEditForBillExec({id: id})
  }).then(res => {
    proxy.$modal.msgSuccess(res.msg);
    getList();
  }).catch(() => {
  });
}


function userSelected(list){
  list.forEach(item => {
    pd.queryParams.userId = item.userId;
  })
}

</script>

<template>
  <div class="app-container">
    <el-form :model="pd.queryParams" ref="queryRef" :inline="true" v-show="pd.showSearch" label-width="68px">
      <el-form-item label="账户" prop="accountId">
        <el-select :style="{width: '120px'}" v-model="pd.queryParams.accountId" placeholder="请选择账户来源" clearable>
          <el-option v-for="item in pd.accountList" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item :label="pd.billType.userName" v-if="pd.billType.userName !== 'hide'" prop="userId">
        <UserSearch :searchType=1 @ok="userSelected"/>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <DictSelect :list="fms_bill_state" v-model:code="pd.queryParams.state"/>
<!--        <el-select :style="{width: '120px'}" v-model="pd.queryParams.state" placeholder="请选择单据状态" clearable>-->
<!--          <el-option v-for="item in fms_bill_state" :key="item.code" :label="item.name" :value="item.code"/>-->
<!--        </el-select>-->
      </el-form-item>
      <el-form-item label="计划时间" style="width: 280px">
        <el-date-picker v-model="pd.daterangePlanTime" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
                        start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="执行时间" style="width: 280px">
        <el-date-picker v-model="pd.daterangeExecutionTime" value-format="YYYY-MM-DD" type="daterange"
                        range-separator="-"
                        start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="单据名称" prop="name">
        <el-input v-model="pd.queryParams.name" placeholder="请输入搜索名称" @keyup.enter="handleQuery"/>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="Search" @click="handleQuery">搜 索</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['fms:bill:add']">新 增</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="pd.showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="pd.loading" :data="pd.billList">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="单据编号" align="center" prop="id"/>
      <el-table-column label="单据类别" align="center" prop="typeName"/>
      <el-table-column :label="pd.billType.userName" align="center" prop="userName"/>
      <el-table-column label="仓储单号" align="center" prop="sourceId"/>
      <el-table-column label="操作员" align="center" prop="userId"/>
      <el-table-column :label="pd.billType.costMoney" align="center" prop="costMoney"/>
      <el-table-column :label="pd.billType.money" align="center" prop="money"/>
      <el-table-column label="状态" align="center" prop="state">
        <template #default="scope">
          <dict-tag :options="fms_bill_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="账户" align="center" prop="accountId">
        <template #default="scope">
          <dict-tag :options="pd.accountList" :value="scope.row.accountId" labelName="name" labelValue="id"/>
        </template>
      </el-table-column>
      <el-table-column :label="pd.billType.planTime" align="center" prop="planTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.planTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="pd.billType.executionTime" align="center" prop="executionTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.executionTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="创建人" align="center" prop="createBy"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updateBy"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="220">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleEdit(scope.row)" v-hasPermi="['fms:bill:edit']">
            修改
          </el-button>
          <el-button link type="primary" icon="DataLine" v-hasPermi="['fms:bill:edit']">
            审批
          </el-button>
          <el-button link type="primary" icon="Check" @click="billExec(scope.row.id)" v-hasPermi="['fms:bill:edit']">
            执行
          </el-button>
          <el-button link type="primary" icon="More" v-hasPermi="['fms:bill:edit']">
            分期
          </el-button>
<!--          <el-button link type="primary" icon="Delete" @click="handleDel(scope.row.id)"-->
<!--                     v-hasPermi="['fms:bill:del']"> 删除  </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="pd.total > 0" :total="pd.total" v-model:page="pd.queryParams.pageNum"
                v-model:limit="pd.queryParams.pageSize" @pagination="getList"/>
    <BillCurd ref="billCurdRef" @ok="billCurdOk"/>
  </div>
</template>

<style lang='scss' scoped>

</style>